package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/17 17:00
 */
public class LeetCode46 {
    /**
     * 给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
     *
     * @param nums
     * @return
     */
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        ArrayList<Integer> objects = new ArrayList<>();
        for (int num : nums) {
            objects.add(num);
        }

        int n = nums.length;

        dfs(n , objects, result, 0);
        return result;
    }

    private void dfs(int nums, List<Integer> objects, List<List<Integer>> result, int first) {
        if (first==nums){
            result.add(new ArrayList<>(objects));
            return;
        }
        for (int i = first; i < nums; i++) {
            // 动态维护数组
            Collections.swap(objects, first, i);
            // 继续递归填下一个数
            dfs(nums, objects, result, first + 1);
            // 撤销操作
            Collections.swap(objects, first, i);
        }

    }

    public static void main(String[] args) {
        LeetCode46 leetcode46 = new LeetCode46();
        System.out.println(leetcode46.permute(new int[]{1, 2, 3}));
    }

}